package zhoufu.sz.sanxiao_check.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import cn.finalteam.galleryfinal.widget.zoonview.PhotoView;
import zhoufu.sz.sanxiao_check.R;

public class ShowPicActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    public static final String _PHOTOS = "Photos";
    ViewPager _VpShowpics;
    TextView _TvPositoinCount;
    private ArrayList<String> _strings;
    private MyPagerAdapter _adapter;
    private int _position;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_showpics);
        _VpShowpics = (ViewPager) findViewById(R.id.vp_showpics);
        _TvPositoinCount = (TextView) findViewById(R.id.tv_positoin_count);
        _strings = getIntent().getStringArrayListExtra(_PHOTOS);
        _position = getIntent().getIntExtra(POSITION, 0);
        _adapter = new MyPagerAdapter(_strings);
        _VpShowpics.setAdapter(_adapter);
        _VpShowpics.addOnPageChangeListener(this);
        if (savedInstanceState != null)
        {
            _position = savedInstanceState.getInt(POSITION);
            _VpShowpics.setCurrentItem(_position, true);
        } else
        {
            _VpShowpics.setCurrentItem(_position);
        }

        _TvPositoinCount.setText(getString(R.string.position_count,_position+1+"",_strings.size()+""));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        _position = position;
        _TvPositoinCount.setText(getString(R.string.position_count, _position + 1+"", _strings.size()+""));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private class MyPagerAdapter extends PagerAdapter {

        private final ArrayList<String> _strings;

        public MyPagerAdapter(ArrayList<String> strings) {
            _strings = strings;
        }

        @Override
        public int getCount() {
            return _strings.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {

            return view == object;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            Context context = container.getContext();
            LayoutInflater from = LayoutInflater.from(context);
            View inflate = from.inflate(R.layout.item_pic, container, false);
            PhotoView photoView = (PhotoView) inflate.findViewById(R.id.photoview);

            Glide.with(context).load(_strings.get(position)).into(photoView);
            container.addView(inflate);
            return inflate;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    public static final String POSITION = "POSITION";

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(POSITION, _position);
        outState.putStringArrayList(_PHOTOS, _strings);
    }


}